﻿using SqlSugar;
using System;
using System.IO;

namespace SuperX.PermissionsService.Data
{
    public class IdentityDbContext
    {
        public SimpleClient<Account> Accounts
        { get { return new SimpleClient<Account>(SqlSugarClient); } }

        public SimpleClient<Role> Roles
        { get { return new SimpleClient<Role>(SqlSugarClient); } }

        public SqlSugarClient SqlSugarClient;

        public IdentityDbContext()
        {
            string dbFilePath = "identity.db";
            if (!File.Exists(dbFilePath))
            {
                File.Create(dbFilePath).Close(); // 关闭文件流
            }
            using (SqlSugarClient = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = "Data Source=" + dbFilePath,
                DbType = DbType.Sqlite,//设置数据库类型
                IsAutoCloseConnection = true,//自动释放数据库，如果存在事务，在事务结束之后释放。
                InitKeyType = InitKeyType.Attribute//从实体特性中读取主键自增列信息
            }))
            {
                //确保表已创建
                SqlSugarClient.CodeFirst.InitTables(typeof(Account), typeof(Role));
                //CreateTable(false, 50, typeof(Account), typeof(Role));
                if (!SqlSugarClient.Queryable<Role>().Any(d => d.Name == "系统管理员"))
                {
                    var adminRole = new Role()
                    {
                        Name = "系统管理员",
                        FunctionMenu = "[\"0\",\"2-4\",\"3-3\",\"1-mesAddress\",\"1-mesSynchronize\",\"1-clear\",\"1-batchDel\",\"1-save\",\"1-del\",\"1-add\",\"1-update\",\"1\",\"2-1\",\"2-1-addDevice\",\"2-1-addNode\",\"2-1-addGroup\"," +
                                       "\"2-1-addTemplate\",\"2-1-exportDevice\",\"2-1-exportGroup\",\"2-1-updateNode\",\"2-1-updateDevice\",\"2-1-updateGroup\",\"2-1-moveDevice\",\"2-1-delNode\",\"2-1-delDevice\",\"2-1-delGroup\",\"2-1-upload\"," +
                                       "\"2-1-add\",\"2-1-batchAdd\",\"2-1-batchDel\",\"2-1-save\",\"2-1-update\",\"2-1-code\",\"2-1-del\",\"2-1-copy\",\"2-2\",\"2-2-lookDevice\",\"2-2-lookGroup\",\"2-3\",\"2-3-save\",\"2-3-add\",\"2-3-update\"," +
                                       "\"2-3-del\",\"2\",\"3-1\",\"3-1-addServer\",\"3-1-addGroup\",\"3-1-lookServer\",\"3-1-updateServer\",\"3-1-updateGroup\",\"3-1-delServer\",\"3-1-delGroup\",\"3-1-add\",\"3-1-save\",\"3-1-look\",\"3-1-update\"," +
                                       "\"3-1-del\",\"3-1-copy\",\"3-2\",\"3-2-addServer\",\"3-2-addGroup\",\"3-2-lookServer\",\"3-2-updateServer\",\"3-2-updateGroup\",\"3-2-delServer\",\"3-2-delGroup\",\"3-2-look\",\"3-2-update\",\"3-2-del\",\"3-2-copy\"," +
                                       "\"3-2-add\",\"3-2-save\",\"3\",\"5\",\"5-1-add\",\"5-1-load\",\"5-1-del\",\"6-1\",\"6-1-reset\",\"6-1-save\",\"6-2\",\"6-2-add\",\"6-2-update\",\"6-2-del\",\"6-3\",\"6-3-add\",\"6-3-update\",\"6-3-password\"," +
                                       "\"6-3-del\",\"6\",\"7\",\"7-1-restart\",\"7-1-obtain\",\"8\",\"8-1-add\",\"8-1-download\",\"8-1-upload\",\"8-1-look\",\"8-1-restart\",\"8-1-update\",\"9-1\",\"9-1-add\",\"9-1-save\",\"9-1-update\",\"9-1-del\",\"9-2\"," +
                                       "\"9-2-add\",\"9-2-save\",\"9-2-update\",\"9-2-del\",\"9\",\"10\",\"10-1-add\",\"10-1-save\",\"10-1-update\",\"10-1-lookElement\",\"10-1-lookProduct\",\"10-1-copy\",\"10-1-del\",\"10-1-batchDel\",\"10-1-addProductElement\"," +
                                       "\"10-1-updateProductElement\",\"10-1-delProductElement\",\"10-1-addProduct\",\"10-1-updateProduct\",\"10-1-delProduct\",\"10-1-lookProductElement\",\"10-1-updateElement\",\"11-1\",\"11-1-addGroup\",\"11-1-updateGroup\"," +
                                       "\"11-1-delGroup\",\"11-1-add\",\"11-1-save\",\"11-1-lookRule\",\"11-1-update\",\"11-1-del\",\"11-1-addRule\",\"11-2\",\"11-2-respondAlarm\",\"11\",\"14\",\"14-1\",\"14-1-save\",\"14-1-reset\",\"4-1\",\"4-2\",\"4-1-reset\"," +
                                       "\"4-1-save\",\"4-3\",\"4-3-addGroup\",\"4-3-updateGroup\",\"4-3-delGroup\",\"4-3-add\",\"4-3-save\",\"4-3-copy\",\"4-3-update\",\"4-3-del\",\"4\"]"
                    };
                    Roles.Insert(adminRole);
                }
                if (!SqlSugarClient.Queryable<Account>().Any(d => d.UserName == "admin"))
                {
                    var adminAccount = new Account()
                    {
                        UserName = "admin",
                        Name = "系统管理员",
                        Password = SHA256Helper.SHA256Encrypt("admin"),
                        RoleName = "系统管理员",
                        RegisterTime = DateTime.Now,
                    };
                    SqlSugarClient.Insertable(adminAccount).ExecuteCommand();
                }
            }
        }

        //private void CreateTable(bool Backup = false, int StringDefaultLength = 50, params Type[] types)
        //{
        //    try
        //    {
        //        SqlSugarClient.CodeFirst.SetStringDefaultLength(StringDefaultLength);
        //        SqlSugarClient.DbMaintenance.CreateDatabase();
        //        if (Backup)
        //        {
        //            SqlSugarClient.CodeFirst.BackupTable().InitTables(types);
        //        }
        //        else
        //        {
        //            SqlSugarClient.CodeFirst.InitTables(types);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Warn($"创建权限用户表失败{ex.Message}");
        //    }
        //}
    }
}